<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreFixfxscate;
use App\Http\Requests\UpdateFixfxscate;
use App\Models\Fixfxs;
use App\Models\Fixfxscate;
use App\Models\Oncustomer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FixfxscateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        view()->share([
            '_ygs' => 'am-in',
            '_fixfxscate' => 'am-active',
        ]);

    }

    public function index()
    {

        $cates = Fixfxscate::orderBy('sort')->get();

        return view('admin.fixfxscate.index', compact('cates'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('/admin.fixfxscate.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreFixfxscate $request)
    {
        $data = $request->all();
        Fixfxscate::create($data);
        return redirect('/admin/fixfxscate')->with('success', '新增成功');

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cate = Fixfxscate::find($id);
        return view('admin.fixfxscate.edit', compact('cate'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateFixfxscate $request, $id)
    {
        $cate = Fixfxscate::find($id);
        $data['name'] = $request->name;
        $info = Fixfxscate::where($data)->first();
        if ($cate['name'] != $request->name && isset($info)) {
            return back()->with('success', '级别已存在');
        } else {
            $about = $request->all();
            $cate->update($about);
            return redirect('/admin/fixfxscate')->with('success', '修改成功');
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $info = Fixfxs::where('fixfxscate_id', $id)->first();
        if ($info) {
            return back()->with('success', '当前分类下有教练，不能删除');
        } else {
            Fixfxscate::destroy($id);
            return back()->with('success', '删除成功');
        }


    }
}
